﻿using System;
using System.Collections.Generic;
using System.Linq;
using yiCommerce.Core.Domain.Customers;
using yiCommerce.Core.Domain.Products;
using yiCommerce.Core.Domain.Statuses;
using yiCommerce.Core.Domain.Tags;

namespace yiCommerce.Service.Statuses
{
    public class StatusMapper
    {
        public static IList<Status> ToStatusesList(IList<Status> statuses)
        {
            if (statuses == null)
            {
                return new List<Status>();
            }
            else
            {
                return statuses.Select(c => ToStatusModel(c,false)).ToList();
            }
        }


        public static Status ToStatusModel(Status status,bool autoLoadComments)
        {
            if (status == null)
            {
                return null;
            }
            else
            {
                return new Status()
                    {
                        Id = status.Id,
                        CreatedAt = status.CreatedAt,
                        Text = status.Text,
                        Point = status.Point,
                        CustomerId = status.CustomerId,
                        Deleted = status.Deleted,
                        Comments = ToComments(status.Comments, autoLoadComments),
                        StatusPictures = ToStatusPictures(status.StatusPictures),
                        IsProductStatus = status.IsProductStatus,
                        Customer = new Customer()
                            {
                                Id = status.Customer.Id,
                                Username = status.Customer.Username,
                                Email = status.Customer.Email,
                                CustomerLogoId = status.Customer.CustomerLogoId,
                                CustomerLogo = status.Customer.CustomerLogo != null ? new CustomerLogo()
                                {
                                    
                                    Id = status.Customer.CustomerLogo.Id,
                                    PictureId = status.Customer.CustomerLogo.PictureId
                                } : null
                            },
                        Product = status.Product == null ? null : new Product()
                        {
                            Id = status.Product.Id,
                            ProductVariants = status.Product.ProductVariants.Select(t => new ProductVariant()
                            {
                                Id=t.Id,
                                StockQuantity = t.StockQuantity,
                                Price = t.Price
                            }).ToList()
                        },
                        StatusTags = status.StatusTags==null?null: ToStatusTags(status.StatusTags),
                    };
            }
        }

        private static ICollection<StatusTag> ToStatusTags(ICollection<StatusTag> statusTags)
        {
            return statusTags.Select(c => new StatusTag()
                {
                    Tag = new Tag()
                        {
                            TagName = c.Tag.TagName,
                            TagValue = c.Tag.TagValue
                        }
                }).ToList();
        }

        public static ICollection<Comment> ToComments(ICollection<Comment> comments, bool autoLoadComments)
        {
            if (comments == null)
                return null;

            if (autoLoadComments)
            {
                return comments.Select(c => ToComment(c)).ToList();
            }
            return null;
        }

       
        public static Comment ToComment(Comment comment)
        {
            if (comment == null)
                return null;
            else
            {
                return new Comment()
                    {
                        StatusId =  comment.StatusId,
                        Status = new Status()
                            {
                                CustomerId = comment.Status.CustomerId
                            },
                        Id = comment.Id,
                        Text = comment.Text,
                        CreatedAt = comment.CreatedAt,

                        Commenter_Id = comment.Commenter_Id,
                        Commenter = new Customer()
                            {
                                CustomerLogoId = comment.Commenter.CustomerLogoId,
                                Id = comment.Commenter.Id,



                                CustomerLogo = comment.Commenter.CustomerLogo != null
                                                   ? new CustomerLogo()

                                                       {
                                                           Id = comment.Commenter.CustomerLogo.Id,
                                                           PictureId = comment.Commenter.CustomerLogo.PictureId,

                                                       }
                                                   : null
                            }
                    };
            }
        }

        private static ICollection<StatusPicture> ToStatusPictures(ICollection<StatusPicture> statusPictures)
        {
            if (statusPictures == null)
            {
                return null;
            }
            else
            {
                return statusPictures.Select(c => new StatusPicture()
                    {
                        PictureId = c.PictureId
                    }).ToList();
            }
        }


        public static IList<Comment> ToCommentList(List<Comment> toList)
        {
            throw new NotImplementedException();
        }

        public static IList<Tag> ToTagList(List<Tag> tags)
        {
            if (tags == null)
            {
                return null;
            }
            else
            {
                return tags.Select(c => new Tag()
                    {
                        CountTaged = c.CountTaged,
                        TagName = c.TagName,
                        TagValue = c.TagValue,
                        CreateAt = c.CreateAt,
                        CreatorId = c.CreatorId,
                        Deleted = c.Deleted

                    }).ToList();
            }
        }
    }
}